// Copyright (C) 1997-2002 Alias|Wavefront,
// a division of Silicon Graphics Limited.
//
// The information in this file is provided for the exclusive use of the
// licensees of Alias|Wavefront.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias|Wavefront license agreement, without fee.
//
// ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
//
global proc createRenderLayerOptionsUI (
    string $parent, string $renderer) 
//
//  Procedure Name:
//      createRenderLayerOptionsUI
//
//  Description:
//      Creates the UI in the "Render Layer/Pass Control" 
//		expand/collapse section for the unified render globals tab
//      for the specified renderer.
//
{
	setUITemplate -pushTemplate attributeEditorTemplate;

	setParent $parent;

	// Set up the spreadsheet filters
	updateRenderLayerFilters;

	// If the UI is created already then just update the attribute values.
	if (`columnLayout -exists ($renderer+"RenderLayerLayout")`) {
		updateRenderLayerOptionsUI($renderer);
		return;
	}

	columnLayout -adjustableColumn true ($renderer+"RenderLayerLayout");

        attrControlGrp -label "Enable Render Layers"
            -attribute "defaultRenderGlobals.renderLayerEnable" 
            -cc ("updateRenderLayerEnableUI(\\\""+$renderer+"\\\")")
            ($renderer+"RenderLayerEnable");

	
        attrControlGrp -label "Enable Render Layer Passes"
            -attribute "defaultRenderGlobals.renderLayerPassEnable"
            -cc ("updateRenderLayerPassEnableUI(\\\""+$renderer+"\\\")")
            ($renderer+"RenderLayerPassEnable");

        attrControlGrp -label "Enable Global Passes"
            -attribute "defaultRenderGlobals.renderGlobalPassEnable" 
            -cc ("updateRenderGlobalPassEnableUI(\\\""+$renderer+"\\\")")
            ($renderer+"RenderGlobalPassEnable");

		separator;

        attrControlGrp -label "Output to Subdirectories"
            -attribute "defaultRenderGlobals.renderLayerSubdirs" 
            ($renderer+"RenderLayerSubdirs");

		frameLayout
			-label "Render Layers"
			-labelVisible false
			-borderVisible false
			-collapsable false
			($renderer+"LayerAttrLayout");

			columnLayout -adjustableColumn true ($renderer+"LayerLyt");

			separator;

 			selectionConnection -wl -parent ($renderer+"LayerLyt") 
                ($renderer+"LayerList");

            // This spreadSheetEditor control is not automatically removed
            // when its parent is removed, so we must ensure that it is
            // deleted before creating it again.
            //
            if ( `spreadSheetEditor -exists ($renderer+"LayerOutliner")` ) {
                deleteUI ($renderer+"LayerOutliner");
            }

            // The spreadSheetEditor lists the renderable attribute of all
            // of the nodes given by aList, after applying the filter,
            // renderLayerFilter.
            //
			spreadSheetEditor 
				-mainListConnection ($renderer+"LayerList")
				-ko false
				-ln true
				-fal {"rndr"}
				-filter renderLayerFilter
				($renderer+"LayerOutliner");

			setParent ..;   // from columnLayout layerLyt

		setParent ..;

	setParent ..;

	setUITemplate -popTemplate;

	// Make sure the values are right
    //
	updateRenderLayerOptionsUI($renderer);
}

global proc updateRenderLayerOptionsUI (string $renderer)
//
//  Procedure Name:
//      updateRenderLayerOptionsUI
//
//  Description:
//      Gets the real values from the option vars and sets the UI based
//		on these values.  This procedure updates all of the RL/RP
//		values.
//
{ 
	int $doLayers = `getAttr "defaultRenderGlobals.renderLayerEnable"`;

	int $doLayerPasses = `getAttr "defaultRenderGlobals.renderLayerPassEnable"`;

	int $doGlobalPasses = `getAttr "defaultRenderGlobals.renderGlobalPassEnable"`;

	int $doSubdirs = `getAttr "defaultRenderGlobals.renderLayerSubdirs"`;
	
	if ( $doLayers ) {
		updateRenderLayerLayoutUI($renderer, true, $doLayerPasses );
	}
	else {
		updateRenderLayerLayoutUI($renderer, false, $doGlobalPasses );
	}	
}

// Description: This procedure is called to update the render layout
//      control.
//
global proc updateRenderLayerLayoutUI(
    string $renderer, int $doLayers, int $doPasses )
{
	updateRenderLayerFilters;

	if ( $doLayers )
	{
		if ( $doPasses )
		{
			spreadSheetEditor
				-e
				-filter renderLayerFilter
				-fal {"rndr","b","cp","s","di","sp"}
				($renderer+"LayerOutliner");

		}
		else
		{
			spreadSheetEditor
				-e
				-filter renderLayerFilter
				-fal {"rndr"}
				($renderer+"LayerOutliner");

		}

        attrControlGrp -edit -enable true ($renderer+"RenderLayerPassEnable");

        attrControlGrp -edit -enable false ($renderer+"RenderGlobalPassEnable");

		frameLayout
			-e
			-m true
			($renderer+"LayerAttrLayout");

	}
	else
	{
		if ( $doPasses )
		{
			spreadSheetEditor
				-e
				-filter glblRenderFilter
				-fal {"b","cp","s","di","sp"}
				($renderer+"LayerOutliner");

			frameLayout
				-e
				-m true
				($renderer+"LayerAttrLayout");

		}
		else
		{
			frameLayout
				-e
				-m false
				($renderer+"LayerAttrLayout");
		}

        attrControlGrp -edit -enable true 
            ($renderer+"RenderGlobalPassEnable");

        attrControlGrp -edit -enable false 
            ($renderer + "RenderLayerPassEnable");
	}
}

// Description: This procedure is called to update the render layer
//      enabled control.
//
global proc updateRenderLayerEnableUI(string $renderer)
{
	int $doPasses;
	int $doLayers = `getAttr defaultRenderGlobals.renderLayerEnable`;
	if ( $doLayers ) {
	    $doPasses = `getAttr defaultRenderGlobals.renderLayerPassEnable`;
	}
	else {
		$doPasses = `getAttr defaultRenderGlobals.renderGlobalPassEnable`; 
	}

	updateRenderLayerLayoutUI($renderer, $doLayers, $doPasses );
}

// Description: This procedure is called to update the render layer
//      pass enabled control.
//
global proc updateRenderLayerPassEnableUI(string $renderer)
{
	int $doLayers = `getAttr defaultRenderGlobals.renderLayerEnable`;
	int $doPasses = `getAttr defaultRenderGlobals.renderLayerPassEnable`;
	updateRenderLayerLayoutUI($renderer, $doLayers, $doPasses );
}

// Description: This procedure is called to update the render globals
//      pass enable control.
//
global proc updateRenderGlobalPassEnableUI(string $renderer)
{
	int $doLayers = `getAttr defaultRenderGlobals.renderLayerEnable`;
	int $doPasses = `getAttr defaultRenderGlobals.renderGlobalPassEnable`; 
	updateRenderLayerLayoutUI($renderer, $doLayers, $doPasses );
}
